import router from "./router";
import store from "./store";
import NProgress from "nprogress"; // Progress 进度条
import "nprogress/nprogress.css"; // Progress 进度条样式
// import {
//     getAuth,
//     getMenu
// } from '@/utils/auth' // 验权
import utils from "@/utils/index.js";

import { Message } from "element-ui";
router.beforeEach((to, from, next) => {
  if (to.meta.title) {
    document.title = to.meta.title;
  }
  try {
    NProgress.start();
    if (to.path === "/login") {
      next();
      NProgress.done();
      return;
    } else if (to.path === "/appChange") {
      next();
      NProgress.done();
      return;
    } else if (to.path === "/noMobile") {
      next();
      NProgress.done();
      return;
    } else if (to.path === "/equipmentCockpit") {
      next();
      NProgress.done();
      return;
    } else if (to.path === "/templatePage") {
      next();
      NProgress.done();
      return;
    } else if (to.path === "/workStation" && to.query.source) {
      utils.clearUserInfo();
      next({
        path: to.path,
        replace: true,
      });
      NProgress.done();
      return;
    }else if (to.path === "/workStation" && to.query.token) {
      utils.clearUserInfo();
      localStorage.setItem("GDH_WATER_TOKEN", to.query.token);
      to.query.currDs && localStorage.setItem("currDs", to.query.currDs);
      to.query.currOrg && localStorage.setItem("currOrg", to.query.currOrg);
      next({
        path: to.path,
        replace: true,
      });
      NProgress.done();
      return;
    } else if (to.name === "defectOrderDetails" && to.query.idInspectResultLog && to.query.token) {
      if (!localStorage.getItem('GDH_WATER_TOKEN')) {
        utils.clearUserInfo();
        localStorage.setItem("GDH_WATER_TOKEN", to.query.token);
        to.query.currDs && localStorage.setItem("currDs", to.query.currDs);
        to.query.currOrg && localStorage.setItem("currOrg", to.query.currOrg);
      }
      let param = Object.assign({}, to.query);
      param.pageType = "add";
      delete param.token;
      next({
        path: to.path,
        query: param,
        replace: true,
      });
      NProgress.done();
      return;
    } else if (to.query.token) {
      utils.clearUserInfo();
      localStorage.setItem("GDH_WATER_TOKEN", to.query.token);
      to.query.currDs && localStorage.setItem("currDs", to.query.currDs);
      to.query.currOrg && localStorage.setItem("currOrg", to.query.currOrg);
      next({
        path: to.path,
        query: {
          id: to.query.id,
          pageType: "details",
        },
        replace: true,
      });
      NProgress.done();
      return;
    } else {
      /**如果没权限先获取 */
      if (!store.getters.userInfo) {
        // 判断当前用户是否获取权限
        store
          .dispatch("GetUserInfo")
          .then((res) => {
            if (JSON.stringify(store.getters.dictData) == "{}") {
              //判断当前用户是否获取数据字典
              store
                .dispatch("GetDictData")
                .then(() => {
                  // 获取字典数据
                  next({
                    ...to,
                    replace: true,
                  });
                  NProgress.done();
                  return;
                })
                .catch((e) => {
                  next();
                  NProgress.done();
                  return;
                });
            } else {
              next();
              NProgress.done();
              return;
            }
            // next();
          })
          .catch((err) => {
            next();
            NProgress.done();
            return;
          });
      } else if (JSON.stringify(store.getters.dictData) == "{}") {
        store
          .dispatch("GetDictData")
          .then(() => {
            // 获取字典数据
            next();
            NProgress.done();
            return;
          })
          .catch((e) => {
            next();
            NProgress.done();
            return;
          });
      } else {
        if (!to.meta.parentCode) {
          store.commit("SET_CURRENT_BTN_AUTH", to.meta.code);
          next();
          NProgress.done();
          return;
        } else {
          let haveAuth = store.getters.isHaveMenuAuth(to.name);
          if (haveAuth) {
            if (!to.name) {
              next();
              NProgress.done();
              return;
            } else {
              if (!to.meta.isOpen) {
                next({
                  path: `/templatePage`,
                  query: {
                    typePage: 2,
                  },
                  replace: true,
                });
              } else if (!to.meta.isEnable) {
                next({
                  path: `/templatePage`,
                  query: {
                    typePage: 3,
                  },
                  replace: true,
                });
              } else {
                store.commit("SET_CURRENT_BTN_AUTH", to.name);
                next();
              }
              NProgress.done();
              return;
            }
          } else {
            next({
              path: `/templatePage`,
              query: {
                typePage: 0,
              },
              replace: true,
            });
            NProgress.done();
            return;
          }
        }
      }
    }
  } catch (error) {
    console.log(error);
    next();

    NProgress.done();
    return;
  }
});
